package com.ruoyi.lowcode.enums;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * SQL 条件构造器
 */
public enum MysqlConditionalConstructor {

    EQUAL("EQUAL", map -> {
        return " and " + map.get("fieldTableAliasName") + " = '" + map.get("value") + "'";
    }),

    LIKE("LIKE", map -> {
        return " and " + map.get("fieldTableAliasName") + " LIKE '%" + map.get("value") + "%'";
    }),

    DATE("DATE", map -> {
        String sql = "";
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if(entry.getKey().contains("start")){
                sql += " and " + map.get("fieldTableAliasName") + " >= '" + entry.getValue() + "'";
            }
            if(entry.getKey().contains("end")){
                sql += " and " + map.get("fieldTableAliasName") + " <= '" + entry.getValue() + "'";
            }
        }
        return sql;
    }),

    GREATER_THAN("GREATER_THAN", map -> {
        return " and " + map.get("fieldTableAliasName") + " > " + map.get("value");
    }),

    LESS_THAN("LESS_THAN", map -> {
        return " and " + map.get("fieldTableAliasName") + " < " + map.get("value");
    });

    private String name;

    private Function<Map<String, Object>, String> conditional;

    MysqlConditionalConstructor(String name, Function<Map<String, Object>, String> conditional) {
        this.name = name;
        this.conditional = conditional;
    }

    public String getConditionalString(Map<String, Object> map){
        return conditional.apply(map);
    }

}
